package com.template.templateuser.domain.mapper;

import com.template.templateuser.domain.dto.SysRoleDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;


public interface SysRoleMapper {
    

    /**
     * 通过id获取角色信息
     *
     * @param id
     * @return
     */
    @Select({
            "<script>",
            "select * from sys_role where deleted=0 and id=#{id}",
            "</script>",
    })
    SysRoleDTO findSysRoleById(@Param("id") Long id);


    /**
     * 保存
     * @param sysRoleDTO
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO sys_role",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"roleName != null\" >",
            "       role_name,",
            "   </if>",
            "   <if test=\"roleKey != null\" >",
            "       role_key,",
            "   </if>",
            "   <if test=\"roleSort != null\" >",
            "       role_sort,",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       status,",
            "   </if>",
            "   <if test=\"createId != null\" >",
            "       create_id,",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       deleted,",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       create_time,",
            "   </if>",
            "   <if test=\"remark != null\" >",
            "       remark,",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       update_time,",
            "   </if>",
            "</trim>",
            "<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"roleName != null\" >",
            "       #{roleName},",
            "   </if>",
            "   <if test=\"roleKey != null\" >",
            "       #{roleKey},",
            "   </if>",
            "   <if test=\"roleSort != null\" >",
            "       #{roleSort},",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       #{status},",
            "   </if>",
            "   <if test=\"createId != null\" >",
            "       #{createId},",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       #{deleted},",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       #{createTime},",
            "   </if>",
            "   <if test=\"remark != null\" >",
            "       #{remark},",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       #{updateTime},",
            "   </if>",
            "</trim>",
            "</script>",
    })
    @SelectKey(resultType = long.class, keyColumn = "id", before = false, statement = "SELECT LAST_INSERT_ID() AS id",
            keyProperty = "id")
    int save(SysRoleDTO sysRoleDTO);

    /**
     * 更新
     *
     * @param sysRoleDTO
     * @return java.lang.Integer
     *
     */
    @Update({
            "<script>",
            "update `sys_role` ",
            "<set > ",
            "  <if test=\"roleName != null\" > ",
            "    role_name = #{roleName}, ",
            "  </if> ",
            "  <if test=\"menuName != null\" > ",
            "    menu_name = #{menuName}, ",
            "  </if> ",
            "  <if test=\"roleKey != null\" > ",
            "    role_key = #{roleKey}, ",
            "  </if> ",
            "  <if test=\"roleSort != null\" > ",
            "    role_sort = #{roleSort}, ",
            "  </if> ",
            "  <if test=\"status != null\" > ",
            "    target = #{status}, ",
            "  </if> ",
            "  <if test=\"deleted != null\" > ",
            "    deleted = #{deleted}, ",
            "  </if> ",
            "  <if test=\"remark != null\" > ",
            "    remark = #{remark}, ",
            "  </if> ",
            "  <if test=\"updateTime != null\" > ",
            "    update_time = #{updateTime}, ",
            "  </if> ",
            "</set> ",
            "   where id = #{id}",
            "</script>",
    })
    Integer updateById(SysRoleDTO sysRoleDTO);


    @Delete({
            "<script>",
                "delete from sys_role_menu where role_id=#{id}",
            "</script>",
    })
    void deleteMenuRole(Long id);

    @Insert({
            "<script>",
            "INSERT INTO sys_role_menu(role_id,menu_id)VALUES",
            "<foreach item=\"item\" index=\"index\" collection=\"menuIds\" open=\"\" separator=\",\" close=\"\">",
            "(#{id},#{item})",
            "</foreach>",
            "</script>",
    })
    int saveMenuRole(@Param("id") Long id, @Param("menuIds") Set<Long> menuIds);

    @Delete({
            "<script>",
            "delete from sys_role_dept where role_id=#{id}",
            "</script>",
    })
    void deleteSaveRoleDept(Long id);
    @Insert({
            "<script>",
            "INSERT INTO sys_role_dept(role_id,dept_id)VALUES",
            "<foreach item=\"item\" index=\"index\" collection=\"deptIds\" open=\"\" separator=\",\" close=\"\">",
            "(#{id},#{item})",
            "</foreach>",
            "</script>",
    })
    int saveRoleDept(@Param("id") Long id, @Param("deptIds")  Set<Long> deptIds);


    @Delete({
            "<script>",
            "delete from sys_user_role where user_id=#{id}",
            "</script>",
    })
    void deleteSaveUserRole(Long id);

    @Insert({
            "<script>",
            "INSERT INTO sys_user_role(user_id,role_id)VALUES",
            "<foreach item=\"item\" index=\"index\" collection=\"roleIds\" open=\"\" separator=\",\" close=\"\">",
            "(#{id},#{item})",
            "</foreach>",
            "</script>",
    })
    int saveUserRole(@Param("id")  Long id,@Param("roleIds")   Set<Long> roleIds);

    /**
     * 等到当前用户的所有角色标示
     * @param userId
     * @return
     */
    @Select({
            "SELECT r.`role_key` FROM  `sys_user_role` ur JOIN `sys_role` r ON ur.`role_id`=r.`id` WHERE ur.`user_id`=#{userId} "
    })
    List<String> getUserRolekeyList(@Param("userId")Long userId);

    /**
     * 等到当前用户的所有菜单标示
     * @param userId
     * @return
     */
    @Select({
            "SELECT m.`menu_key` FROM  `sys_role_menu` rm JOIN `sys_menu` m ON rm.`role_id`=m.`id` WHERE rm.`user_id``=#{userId} "
    })
    List<String> getUserMenukeyList(@Param("userId")Long userId);
}
